package com.example.sonny.dutest;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.sonny.Constraint;
import com.example.sonny.dutest.interfaces.BaseActivityI;
import com.example.sonny.util.AsyncHttpClientUtil;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.tt.whorlviewlibrary.WhorlView;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;



public class SignInActivity extends BaseActivity implements BaseActivityI{

    private Button btn_sign_in;
    private EditText email_et;
    private EditText password_et;
    private WhorlView whorsignin;


    private String email;
    private String pass;

    private SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in_screen);

        initView();

        initListener();
    }


    @Override
    public void preInit() {

    }

    @Override
    public void initView() {
        btn_sign_in = (Button)findViewById(R.id.btnSingIn);
        email_et = (EditText)findViewById(R.id.etUserName);
        password_et =(EditText)findViewById(R.id.etPass);
        whorsignin = (WhorlView)findViewById(R.id.whorsignin);
        whorsignin.setVisibility(View.INVISIBLE);
    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {

        btn_sign_in.setOnClickListener(this);
    }

    private void signInJob() {
        email = email_et.getText().toString();
        pass = password_et.getText().toString();
        final RequestParams requestParams = new RequestParams();
        requestParams.add("email",email);
        requestParams.add("password", Constraint.md5(pass));
        if(!email.equalsIgnoreCase("")&&!pass.equalsIgnoreCase("")){
            AsyncHttpClient client = AsyncHttpClientUtil.getInstence();

            client.post(Constraint.API_URL + "api/authenticate", requestParams, new JsonHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                    super.onSuccess(statusCode, headers, response);
                    try {

                        sp = getSharedPreferences("sign", 0);
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putString("email", response.getString("email"));
                        editor.commit();
                        token = response.getString("email");
                        Toast.makeText(getApplicationContext(), "登录成功" + response.getString("email"), Toast.LENGTH_LONG).show();
                        whorsignin.stop();
                        whorsignin.setVisibility(View.INVISIBLE);
                        Intent intent = new Intent();
                        intent.setClass(SignInActivity.this, MenuActivity.class);
                        startActivity(intent);
                        finish();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    super.onFailure(statusCode, headers, responseString, throwable);
                    Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_LONG).show();
                    whorsignin.stop();
                    whorsignin.setVisibility(View.INVISIBLE);
                }
            });

        }else if(email.equalsIgnoreCase("")){
            toast("邮箱不能为空！");
            whorsignin.stop();
            whorsignin.setVisibility(View.INVISIBLE);
        }else{
            toast("密码不能为空！");
            whorsignin.stop();
            whorsignin.setVisibility(View.INVISIBLE);
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnSingIn:
                whorsignin.setVisibility(View.VISIBLE);
                whorsignin.start();
                signInJob();
                break;
        }

    }
}
